package com.zmn.oms.business.impl.work.customwork;

import com.alibaba.fastjson.JSON;
import com.zmn.base.engineer.common.constants.DubboConsts;
import com.zmn.base.engineer.common.dro.supervisor.SupervisorBasicDRO;
import com.zmn.base.engineer.dubbo.interfaces.supervisor.EngineerSupervisorListRemoteService;
import com.zmn.common.constant.GlobalConsts;
import org.apache.dubbo.config.annotation.Reference;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.business.interfaces.master.OrderMasterBService;
import com.zmn.oms.business.interfaces.work.customwork.CustomWorkBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.work.modify.OrderDistributeDTO;
import com.zmn.oms.model.entity.master.OrderMaster;
import com.zmn.oms.model.entity.order.Order;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.master.OrderMasterService;
import com.zmn.oms.services.interfaces.order.OrderService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：客服工单业务实现
 *
 * @author liuying
 * @date 2018/11/02 21:02
 */
@Service
public class CustomWorkBServiceImpl implements CustomWorkBService {
    private Logger logger = LoggerFactory.getLogger(CustomWorkBServiceImpl.class);

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderMasterService orderMasterService;
    @Autowired
    private OrderMasterBService orderMasterBService;
    @Autowired
    private OrderWorkService orderWorkService;

   // @Reference(version = com.zmn.base.engineer.common.constants.DubboConsts.INTERFACE_VERSION, check = false)
    //private SupervisorListRemoteService supervisorListRemoteService;
    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private EngineerSupervisorListRemoteService engineerSupervisorListRemoteService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveDistribute(OrderDistributeDTO orderDistributeDTO) throws OmsBaseException {

        // 删除当前无效的工程师
        orderMasterService.deleteMaster(orderDistributeDTO.getOrderId(), orderDistributeDTO.getWorkId(), OrderConsts.ORDER_MASTER_TYPE_MAIN);
        orderMasterService.deleteMasterByMasterId(orderDistributeDTO.getOrderId(), orderDistributeDTO.getWorkId(), orderDistributeDTO.getMasterId());

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderDistributeDTO.getOrderId(), orderDistributeDTO.getWorkId());

        // 新增主工程师
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setOrderId(orderDistributeDTO.getOrderId());
        orderMaster.setWorkId(orderDistributeDTO.getWorkId());
        orderMaster.setMasterId(orderDistributeDTO.getMasterId());
        orderMaster.setMasterPhone(orderDistributeDTO.getMasterPhone());
        orderMaster.setMasterName(orderDistributeDTO.getMasterName());
        orderMaster.setCreater(orderDistributeDTO.getOperator());
        orderMaster.setCreateTime(orderDistributeDTO.getDistributeDate());
        orderMaster.setStarLevel(GlobalConsts.NONE);
        orderMasterBService.insertOrderMaster(orderMaster);

        // 更新订单工程师
        Order order = new Order();
        order.setOrderId(orderDistributeDTO.getOrderId());
        order.setStatus(OrderStatusConsts.WORK_STATUS_DISTRIBUTE);
        order.setMasterName(orderDistributeDTO.getMasterName());
        order.setUpdater(orderDistributeDTO.getOperator());
        order.setUpdateTime(orderDistributeDTO.getDistributeDate());
        orderService.updateOrderByKey(order);

        // 更新工单工程师
        OrderWork updateOrderWork = new OrderWork();
        updateOrderWork.setOrderId(orderDistributeDTO.getOrderId());
        updateOrderWork.setWorkId(orderDistributeDTO.getWorkId());
        updateOrderWork.setStatus(OrderStatusConsts.WORK_STATUS_DISTRIBUTE);
        updateOrderWork.setMasterId(orderDistributeDTO.getMasterId());
        updateOrderWork.setMasterName(orderDistributeDTO.getMasterName());
        updateOrderWork.setServItemType(orderDistributeDTO.getServItemType());
        updateOrderWork.setFinalPrice(orderDistributeDTO.getFinalPrice());

        OrderWork dbOrderWork = orderDistributeDTO.getDbOrderWork();
        if (NumberUtil.isNotNullOrZero(dbOrderWork.getServProductGroupId())) {
            // 查询并保存工程师主管
            // todo fubiao
            logger.info("saveDistribute#getSupervisorByIdAndProductGroupId 入参：masterId={}，bizType={}，servProductGroupId={}", orderDistributeDTO.getMasterId(), dbOrderWork.getBizType(), dbOrderWork.getServProductGroupId());
            ResponseDTO<SupervisorBasicDRO> remoteRespDTO = engineerSupervisorListRemoteService.getSupervisorByIdAndProductGroupId(orderDistributeDTO.getMasterId(), dbOrderWork.getServProductGroupId());
            logger.info("saveDistribute#getSupervisorByIdAndProductGroupId 出参：remoteRespDTO={}", JSON.toJSONString(remoteRespDTO));
            if (remoteRespDTO.isSuccess() && remoteRespDTO.getData() != null) {
                SupervisorBasicDRO supervisorBasicDRO = remoteRespDTO.getData();
                updateOrderWork.setMasterSupervisorId(supervisorBasicDRO.getSupervisorId());
                updateOrderWork.setMasterSupervisorName(supervisorBasicDRO.getSupervisorName());
                updateOrderWork.setMasterSupervisorPhone(supervisorBasicDRO.getMobile());
            }
        }

        if (updateOrderWork.getMasterSupervisorId() == null) {
            updateOrderWork.setMasterSupervisorId(0);
            updateOrderWork.setMasterSupervisorName("");
            updateOrderWork.setMasterSupervisorPhone("");
        }

        // 派单员
        if (Objects.nonNull(orderDistributeDTO.getOperatorId())) {
            updateOrderWork.setDistributerId(orderDistributeDTO.getOperatorId());
            updateOrderWork.setDistributerType(orderDistributeDTO.getOperatorType());
        }

        // 派单时间
        updateOrderWork.setDistributeTime(orderDistributeDTO.getDistributeDate());
        updateOrderWork.setUpdater(orderDistributeDTO.getOperator());
        updateOrderWork.setUpdateTime(orderDistributeDTO.getDistributeDate());

        orderWorkService.saveDistribute(updateOrderWork);
    }

}